﻿using CRM_S.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace CRM_S.Controllers
{
    public class RoleController : ApiController
    {
        // GET: api/Role
        [HttpGet]
        public IHttpActionResult user(string rname, int page = 1, int limit = 10)
        {
            using (cremEntities db = new cremEntities())
            {
                var datalist = db.Roles.AsEnumerable();
                if (!string.IsNullOrEmpty(rname))
                {
                    datalist = datalist.Where(p => p.rname.Contains(rname));
                }
                var count = datalist.Count();
                var list = datalist.Select(p => new {
                    rid = p.rid,
                    rname = p.rname
                }).Skip((page - 1) * limit).Take(limit).ToList();
                return Json(new { count = count, data = list });
            }
        }
        [HttpPost]
        public IHttpActionResult addUsers([FromBody]Role add)
        {
            using (cremEntities db = new cremEntities())
            {
                db.Roles.Add(add);
                int n = db.SaveChanges();
                if (n > 0)
                {
                    return Json(new { code = 0, msg = "添加成功！" });
                }
                else
                {
                    return Json(new { code = -1, msg = "添加失败！" });
                }
            }
        }
        [HttpPost]
        public IHttpActionResult Edit([FromBody]Role add)
        {
            using (cremEntities db = new cremEntities())
            {
                var list = db.Roles.FirstOrDefault(p => p.rid == add.rid);
                list.rname = add.rname;
                int n = db.SaveChanges();
                if (n > 0)
                {
                    return Json(new { code = 0, msg = "修改成功！" });
                }
                else
                {
                    return Json(new { code = -1, msg = "修改失败！" });
                }
            }
        }
        [HttpPost]
        public IHttpActionResult Delete([FromBody]Role user)
        {
            using (cremEntities db = new cremEntities())
            {
                var list = db.Roles.FirstOrDefault(p => p.rid == user.rid);
                db.Roles.Remove(list);
                int n = db.SaveChanges();
                if (n > 0)
                {
                    return Json(new { code = 0, msg = "删除成功！" });
                }
                else
                {
                    return Json(new { code = -1, msg = "删除失败！" });
                }
            }
        }

        /// <summary>
        /// 模块
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult GetAllModules()
        {
            using (cremEntities db = new cremEntities())
            {
                var datalist = db.Modules.Where(p => p.ParentId == 0).ToList();
                var bb = datalist.Select(p => new
                {
                    Id = p.Id,
                    Name = p.Name,
                    ParentId = p.ParentId,
                    Path = p.Path,
                    Weight = p.Weight,
                }).ToList();
                List<ModuleList> data = new List<ModuleList>();
                foreach (var item in bb)
                {
                    var list = db.Modules.Where(p => p.ParentId == item.Id).ToList();
                    var cc = list.Select(p => new Module()
                    {
                        Id = p.Id,
                        Name = p.Name,
                        ParentId = p.ParentId,
                        Path = p.Path,
                        Weight = p.Weight                      
                    }).ToList();
                    var aa = new ModuleList()
                    {
                        Id = item.Id,
                        Name = item.Name,
                        ParentId = Convert.ToInt32(item.ParentId),
                        Path = item.Path,
                        Weight = item.Weight,
                        children = cc
                    };
                    data.Add(aa);
                };
                return Json(new { data = data, code = 0 });
            }
        }
        [HttpPost]
        public IHttpActionResult GetMyModules([FromBody] Role role)
        {
            using (cremEntities db = new cremEntities())
            {
                var datalist = db.RoleModules.Where(p => p.RoleId == role.rid).ToList();
                var list = new List<int>();
                foreach (var item in datalist)
                {
                    list.Add(item.ModuleId);
                }
                return Json(new { data = list, code = 0 });
            }
        }

        /// <summary>
        /// 模块编辑
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="Value"></param>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult EditRM(int Id, string Value)
        {
            using (cremEntities db = new cremEntities())
            {
                int n = 0;
                int id = Id;
                if (Value != null)
                {
                    string[] strArray = Value.Split(','); //字符串转数组
                    List<int> abc = new List<int>();
                    foreach (var g in strArray)
                    {
                        abc.Add(Convert.ToInt32(g));
                    }
                    var datalist = db.RoleModules.Where(p => p.RoleId ==Id).ToList();
                    foreach (var item in datalist)
                    {
                        db.RoleModules.Remove(item);
                        n += db.SaveChanges();
                    }
                    foreach (var i in abc)
                    {
                        var dd = new RoleModule()
                        {
                            RoleId =Id,
                            ModuleId = i
                        };
                        db.RoleModules.Add(dd);
                        n += db.SaveChanges();
                    }
                }
                else
                {
                    var datalist = db.RoleModules.Where(p => p.RoleId ==Id).ToList();
                    foreach (var item in datalist)
                    {
                        db.RoleModules.Remove(item);
                        n += db.SaveChanges();
                    }
                }
                if (n >= 0)
                {
                    return Json(new { code = 0, msg = "修改成功！" });
                }
                else
                {
                    return Json(new { code = -1, msg = "修改失败！" });
                }
            }
        }
    }
}
